<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>
    </style>
</head>
<body>
<div id="app">
    <!--页头组件-->
    <el-page-header style="background-color: #0095d7;color: white;line-height: 60px" @back="goBack" content="添加轮播图">
    </el-page-header>
    <el-divider></el-divider>
    <el-card>
        <!--添加上传文件的组件-->
        <!--action代表提交地址 name代表文件上传时的参数名称
        limit用来限制图片数量
        -->

        <el-upload
                action="/upload"
                :limit="1"
                name="picFile"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
        >
            <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
    </el-card>
    <el-button @click="insert()">发布轮播图</el-button>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script>
   let v = new Vue({
        el: '#app',
        data: function (){
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                url:""
            }
            },
       methods:{
            goBack(){
                history.back();
            },
           handleRemove(file, fileList) {
               console.log(file, fileList);
               //发请求告诉服务器删除文件夹里面的文件
               //得到要删除的文件名
               let fileName= file.response;
               console.log("文件名:"+fileName);
               axios.get("/remove?fileName="+fileName).then(function () {
                   console.log("服务器文件删除完成!");
               })
               v.url="";
           },
           handlePictureCardPreview(file) {
               this.dialogImageUrl = file.url;
               this.dialogVisible = true;
           },
           insert(){
                if (v.url==""){
                    v.$message.error("请选择上传的图片!");
                    return;
                }
               //发出异步请求
               axios.get("/lbt/insert?url="+v.url).then(function () {
                   location.href="/admin3.html";
               })
           },
           handleSuccess(response,file,fileList){
                console.log("上传文件成功!")
               console.log("response="+response)
               console.log("file="+file)
               v.url=response;
           }
            }
    })
</script>
</html>